package com.sh.shanhuo.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.media.MediaSelector;
import com.example.media.bean.MediaSelectorFile;
import com.example.media.resolver.Contast;
import com.sh.shanhuo.R;
import com.sh.shanhuo.bean.BaseBean;
import com.sh.shanhuo.bean.UploadImageBean;
import com.sh.shanhuo.dialog.CommonEditDialog;
import com.sh.shanhuo.listener.SimpleResultCallback;
import com.sh.shanhuo.listener.GsonObjectCallback;
import com.sh.shanhuo.utils.OkHttpUtil;
import com.sh.shanhuo.view.CircleImageView;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class PersonalMoreActivity extends BaseActivity {

    private CircleImageView image;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal_more;
    }

    @Override
    protected void initView() {
        super.initView();
        ImageView back = findViewById(R.id.back);
        image = findViewById(R.id.image);
        RelativeLayout nickNameLayout = findViewById(R.id.nickNameLayout);
        RelativeLayout imageLayout = findViewById(R.id.imageLayout);
        TextView nickName = findViewById(R.id.nickName);
        TextView account = findViewById(R.id.account);
        back.setOnClickListener(v -> finish());

        account.setText(PersonalInfoActivity.loginBean.getData().getUserName());
        nickName.setText(PersonalInfoActivity.loginBean.getData().getNickName());
        Glide.with(PersonalMoreActivity.this)
                .load(PersonalInfoActivity.loginBean.getData().getHeadUrl())
                .placeholder(R.drawable.person_image)
                .error(R.drawable.person_image).into(image);

        nickNameLayout.setOnClickListener(v ->
                CommonEditDialog.updateUserInfo(PersonalMoreActivity.this,
                        PersonalInfoActivity.loginBean.getData().getNickName(),
                        new SimpleResultCallback() {
                            @Override
                            public void onResult(String data1, String data2) {

                            }

                            @Override
                            public void onButton(String action) {
                                nickName.setText(action);
                                updateUserInfo(action, "");
                            }

                            @Override
                            public void onBean(Object obj) {

                            }
                        }));

        imageLayout.setOnClickListener(v -> {
            MediaSelector.MediaOptions mediaOptions = new MediaSelector.MediaOptions();
            //是否要显示拍照功能
            mediaOptions.isShowCamera = true;
            //是否要压缩
            mediaOptions.isCompress = true;
            //是否要显示视频文件
            mediaOptions.isShowVideo = false;
            //设置module主题
            mediaOptions.themeColor = R.color.black;
            //设置要不要裁剪（视频不裁剪、单图选择接受裁剪，裁剪大小自己可以设置）
            mediaOptions.isCrop = true;
            mediaOptions.cropWidth = 100;
            mediaOptions.cropHeight = 100;
            MediaSelector.with(PersonalMoreActivity.this).setMediaOptions(mediaOptions).openMediaActivity();
        });
    }

    private void updateUserInfo(String nickName, String headUrl) {
        onShowLoading();
        Map<String, String> params = new HashMap<>();
        params.put("id", PersonalInfoActivity.loginBean.getData().getId() + "");
        params.put("userName", PersonalInfoActivity.loginBean.getData().getUserName());
        params.put("password", PersonalInfoActivity.loginBean.getData().getPassword());
        if (!TextUtils.isEmpty(nickName)) {
            params.put("nickName", nickName);
        }
        if (!TextUtils.isEmpty(headUrl)) {
            params.put("headUrl", headUrl);
        }
        params.put("phone", PersonalInfoActivity.loginBean.getData().getPhone());
        params.put("email", PersonalInfoActivity.loginBean.getData().getEmail());
        params.put("uuid", PersonalInfoActivity.loginBean.getData().getUuid());
        OkHttpUtil.getInstance().postForm(OkHttpUtil.IP + OkHttpUtil.UPDATE_USER, params, new GsonObjectCallback<BaseBean>() {
            @Override
            public void onUi(BaseBean baseBean) {
                onDismissLoading();
                if (baseBean.getCode() == 200 && "操作成功".equals(baseBean.getMsg())) {
                    Toast.makeText(PersonalMoreActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(PersonalMoreActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailed(Call call, IOException e) {
                onDismissLoading();
                Toast.makeText(PersonalMoreActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Contast.CODE_RESULT_MEDIA && requestCode == Contast.CODE_REQUEST_MEDIA) {
            onShowLoading();
            List<MediaSelectorFile> mediaList = MediaSelector.resultMediaFile(data);
            MediaSelectorFile file = mediaList.get(0);
            Glide.with(this).load(file.filePath).into(image);
            OkHttpUtil.getInstance().postFile(OkHttpUtil.IP + OkHttpUtil.UPLOAD_IMAGE, new File(file.filePath), new GsonObjectCallback<UploadImageBean>() {
                @Override
                public void onUi(UploadImageBean uploadImageBean) {
                    onDismissLoading();
                    updateUserInfo("", uploadImageBean.getUrl());
                }

                @Override
                public void onFailed(Call call, IOException e) {
                    Toast.makeText(PersonalMoreActivity.this, "上传头像失败", Toast.LENGTH_SHORT).show();
                    onDismissLoading();
                }
            });
        }
    }
}